
import SpublicContainer from '@/core/backendjsfunc/spublic.js'
import FooterContainer from '@/core/backendjsfunc/footer.js'
import Component from '@/core/backendjsfunc/component.js'
import Drag from '@/core/backendjsfunc/drag.js'
import Container from '@/core/backendjsfunc/container.js'
import FormComponent from '@/core/backendjsfunc/formComponent.js'
import FormContainer from '@/core/backendjsfunc/formContainer.js'
import CustomContainer from '@/core/backendjsfunc/customContainer.js'
import SimpleComponent from '@/core/backendjsfunc/simpleComponent.js'
import MenuFootfixedComponent from '@/core/backendjsfunc/menuFootfixedComponent.js'

import { backendjsFunctions } from '@/core/functions.js'

export default {
  _verification (functionName) {
    var bjFunc = backendjsFunctions()
    if (bjFunc === false) {
      return false
    }
    if (!bjFunc[functionName] || typeof bjFunc[functionName] !== 'function') {
      throw new Error('iframe 不存在 backendjsFunctions 对象的 ' + functionName + ' 方法！')
    }
    return true
  },
  /**
   * 切换显示pc、tel显示设备，对前台渲染进行改变
   * @param {*} 设备标识 1-pc 2-tel
   * @returns 无返回
   */
  changeDevice (device) {
    this._verification('changeDevice')
    try {
      return backendjsFunctions().changeDevice(device)
    } catch (error) {
      console.error(error)
    }
    return null
  },
  /**
   * 通过组件的名称获取到组件的默认数据
   * @param {*} COMPONENT_NAME 组件的名称
   * @returns 返回组件的默认数据或者空数据
   */
  componentDefaultData (COMPONENT_NAME) {
    this._verification('componentDefaultData')
    try {
      return backendjsFunctions().componentDefaultData(COMPONENT_NAME)
    } catch (error) {
      console.error(error)
    }
    return null
  },
  /**
   * logo编辑弹窗保存方法
   * @param {*} bjComponent 弹窗开启时传递过来的参数
   * @returns
   */
  saveLogo (bjComponent) {
    this._verification('saveLogo')
    return new Promise((resolve, reject) => {
      try {
        backendjsFunctions().saveLogo(bjComponent)
        resolve(bjComponent)
      } catch (error) {
        reject(error)
      }
    })
  },
  /**
   * 获取 backendjs 当前的 logo 渲染数据
   * @returns
   */
  getLogo () {
    this._verification('getLogo')
    try {
      return backendjsFunctions().getLogo()
    } catch (error) {
      console.error(error)
    }
    return null
  },
  /**
   * banner 编辑弹窗保存方法
   * @param {*} data 弹窗开启时传递过来的参数
   * @returns
   */
  saveBanner (data) {
    this._verification('saveBanner')
    return new Promise((resolve, reject) => {
      try {
        backendjsFunctions().saveBanner(data)
        resolve(data)
      } catch (error) {
        reject(error)
      }
    })
  },
  /**
   * 获取 backendjs 当前的 banner 渲染数据
   * @returns
   */
  getBanner () {
    this._verification('getBanner')
    try {
      return backendjsFunctions().getBanner()
    } catch (error) {
      console.error(error)
    }
    return null
  },
  renderBannerAgain () {
    this._verification('renderBannerAgain')
    try {
      return backendjsFunctions().renderBannerAgain()
    } catch (error) {
      console.error(error)
    }
    return null
  },
  /**
   * navigation编辑弹窗保存方法
   * @param {*} bjComponent 弹窗开启时传递过来的参数
   * @returns
   */
  saveNavigation (bjComponent) {
    this._verification('saveNavigation')
    return new Promise((resolve, reject) => {
      try {
        backendjsFunctions().saveNavigation(bjComponent)
        resolve(bjComponent)
      } catch (error) {
        reject(error)
      }
    })
  },
  /**
   * 获取 backendjs 当前的 navigation 渲染数据
   * @returns
   */
  getNavigation () {
    this._verification('getNavigation')
    try {
      return backendjsFunctions().getNavigation()
    } catch (error) {
      console.error(error)
    }
    return null
  },
  rerenderNavigation (listNavigation) {
    this._verification('rerenderNavigation')
    try {
      return backendjsFunctions().rerenderNavigation(listNavigation)
    } catch (error) {
      console.error(error)
    }
    return null
  },
  /**
   * 是否是普通页面
   * @returns
   */
  isCustom () {
    this._verification('isCustom')
    try {
      return backendjsFunctions().isCustom()
    } catch (error) {
      console.error(error)
    }
    return null
  },
  /**
   * 获取页面导航的 listNavigation 数据
   * @returns
   */
  getListNavigationData () {
    this._verification('getListNavigationData')
    try {
      return backendjsFunctions().getListNavigationData()
    } catch (error) {
      console.error(error)
    }
    return null
  },
  ...SpublicContainer,
  ...FooterContainer,
  ...Component,
  ...Drag,
  ...Container,
  ...FormComponent,
  ...FormContainer,
  ...CustomContainer,
  ...SimpleComponent,
  ...MenuFootfixedComponent,
  /**
   * 显示 containerCol 区域背景
   * @param {*} tmpId containerCol 唯一标识
   * @returns
   */
  showContainerCol (tmpId) {
    this._verification('showContainerCol')
    try {
      return backendjsFunctions().showContainerCol(tmpId)
    } catch (error) {
      console.error(error)
    }
    return null
  },
  /**
   * 隐藏 containerCol 区域背景
   * @param {*} tmpId containerCol 唯一标识
   * @returns
   */
  hideContainerCol (tmpId) {
    this._verification('hideContainerCol')
    try {
      return backendjsFunctions().hideContainerCol(tmpId)
    } catch (error) {
      console.error(error)
    }
    return null
  },
  /**
   * 隐藏所有 containerCol 区域背景
   * @returns
   */
  hideAllContainerCol () {
    this._verification('hideAllContainerCol')
    try {
      return backendjsFunctions().hideAllContainerCol()
    } catch (error) {
      console.error(error)
    }
    return null
  },
  /**
   * 滚动条定位到 containerCol 区域
   * @param {*} tmpId containerCol 唯一标识
   * @returns
   */
  scrollContainerCol (tmpId) {
    this._verification('scrollContainerCol')
    try {
      return backendjsFunctions().scrollContainerCol(tmpId)
    } catch (error) {
      console.error(error)
    }
    return null
  },
  /**
   * Vue 选择完毕模板的主题颜色后，调用此方法在 backendjs 端进行修改颜色 css 文件
   * @param {*} newColor
   * @param {*} callback
   * @returns
   */
  updateSiteThemeColor (newColor, callback) {
    this._verification('updateSiteThemeColor')
    try {
      return backendjsFunctions().updateSiteThemeColor(newColor, callback)
    } catch (error) {
      console.error(error)
    }
    return null
  },
  // 执行发布动作
  publish () {
    this._verification('publish')
    try {
      return backendjsFunctions().publish()
    } catch (error) {
      console.error(error)
    }
    return null
  }
}
